package handler

import (
	"net/http"
	"strings"

	"mall/service/recommended/api/internal/logic"
	"mall/service/recommended/api/internal/svc"
	"mall/service/recommended/api/internal/types"

	"github.com/zeromicro/go-zero/rest/httpx"
)

func GetItemPopularHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		req := types.ItemSearchReq{}
		var err error
		if r.Form["uid"] != nil && len(r.Form["uid"]) > 0 && len(strings.TrimSpace(r.Form["uid"][0])) > 0 {
			req.UserId = r.Form["uid"][0]
		}
		if r.Form["offset"] != nil && len(r.Form["offset"]) > 0 && len(strings.TrimSpace(r.Form["offset"][0])) > 0 {
			req.Offset = r.Form["offset"][0]
		}
		if r.Form["pageSize"] != nil && len(r.Form["pageSize"]) > 0 && len(strings.TrimSpace(r.Form["pageSize"][0])) > 0 {
			req.PageSize = r.Form["pageSize"][0]
		}
		
		l := logic.NewGetItemPopularLogic(r.Context(), svcCtx)
		resp, err := l.GetItemPopular(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
